package com.yanyeori.framework.jira.jiraclient.greenhopper;

import com.yanyeori.framework.jira.jiraclient.Field;
import com.yanyeori.framework.jira.jiraclient.JiraException;
import com.yanyeori.framework.jira.jiraclient.RestClient;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * GreenHopper backlog data.
 */
public class Backlog {

    private RestClient restclient;
    private List<SprintIssue> issues = null;
    private List<SprintIssue> backlogIssues = null;
    private int rankCustomFieldId = 0;
    private List<Sprint> sprints = null;
    private List<RapidViewProject> projects = null;
    private List<Marker> markers = null;
    private List<Epic> epics = null;
    private boolean canEditEpics = false;
    private boolean canManageSprints = false;
    private boolean maxIssuesExceeded = false;
    private int queryResultLimit = 0;
    private Map<String, List<RapidViewVersion>> versionsPerProject = null;

    /**
     * Creates the backlog from a JSON payload.
     *
     * @param restclient REST client instance
     * @param json       JSON payload
     */
    protected Backlog(RestClient restclient, JSONObject json) {
        this.restclient = restclient;

        if (json != null)
            deserialise(json);
    }

    private void deserialise(JSONObject json) {
        issues = GreenHopperField.getResourceArray(SprintIssue.class, json.get("issues"), restclient);
        rankCustomFieldId = Field.getInteger(json.get("rankCustomFieldId"));
        sprints = GreenHopperField.getResourceArray(Sprint.class, json.get("sprints"), restclient);
        projects = GreenHopperField.getResourceArray(RapidViewProject.class, json.get("projects"), restclient);
        markers = GreenHopperField.getResourceArray(Marker.class, json.get("markers"), restclient);
        canManageSprints = Field.getBoolean(json.get("canManageSprints"));
        maxIssuesExceeded = Field.getBoolean(json.get("maxIssuesExceeded"));
        queryResultLimit = Field.getInteger(json.get("queryResultLimit"));

        if (json.containsKey("epicData") && json.get("epicData") instanceof JSONObject) {
            JSONObject epicData = (JSONObject) json.get("epicData");
            epics = GreenHopperField.getResourceArray(Epic.class, epicData.get("epics"), restclient);
            canEditEpics = Field.getBoolean(epicData.get("canEditEpics"));
        }

        if (json.containsKey("versionData") && json.get("versionData") instanceof JSONObject) {
            JSONObject verData = (JSONObject) json.get("versionData");

            if (verData.containsKey("versionsPerProject") && verData.get("versionsPerProject") instanceof JSONObject) {
                JSONObject verMap = (JSONObject) verData.get("versionsPerProject");
                versionsPerProject = new HashMap<>();

                for (Map.Entry<String, Object> kvp : (Iterable<Map.Entry<String, Object>>) verMap.entrySet()) {
                    if (!(kvp.getValue() instanceof JSONArray))
                        continue;

                    List<RapidViewVersion> versions = new ArrayList<>();

                    for (Object item : (JSONArray) kvp.getValue()) {
                        if (!(item instanceof JSONObject))
                            continue;

                        RapidViewVersion ver = new RapidViewVersion(restclient, (JSONObject) item);
                        versions.add(ver);
                    }

                    versionsPerProject.put(kvp.getKey(), versions);
                }
            }
        }

        //determining which issues are actually in the backlog vs the sprints
        //fill in the issues into the single sprints and the backlog issue list respectively
        for (SprintIssue issue : issues) {
            boolean addedToSprint = false;
            for (Sprint sprint : sprints) {
                if (sprint.getIssuesIds().contains(issue.getId())) {
                    sprint.getIssues().add(issue);
                    addedToSprint = true;
                }
            }
            if (!addedToSprint) {
                if (backlogIssues == null) {
                    backlogIssues = new ArrayList<>();
                }
                backlogIssues.add(issue);
            }
        }
    }

    /**
     * Retrieves the backlog data for the given rapid view.
     *
     * @param restclient REST client instance
     * @param rv         Rapid View instance
     * @return the backlog
     * @throws JiraException when the retrieval fails
     */
    public static Backlog get(RestClient restclient, RapidView rv) throws JiraException {
        final int rvId = rv.getId();
        JSON result;

        try {
            URI reporturi = restclient.buildURI(
                    GreenHopperResource.RESOURCE_URI + "xboard/plan/backlog/data",
                    new HashMap<String, String>() {{
                        put("rapidViewId", Integer.toString(rvId));
                    }});
            result = restclient.get(reporturi);
        } catch (Exception ex) {
            throw new JiraException("Failed to retrieve backlog data", ex);
        }

        if (!(result instanceof JSONObject))
            throw new JiraException("JSON payload is malformed");

        return new Backlog(restclient, (JSONObject) result);
    }

    public List<SprintIssue> getIssues() {
        return issues;
    }

    public List<SprintIssue> getBacklogIssues() {
        return backlogIssues;
    }

    public int getRankCustomFieldId() {
        return rankCustomFieldId;
    }

    public List<Sprint> getSprints() {
        return sprints;
    }

    public List<RapidViewProject> getProjects() {
        return projects;
    }

    public List<Marker> getMarkers() {
        return markers;
    }

    public List<Epic> getEpics() {
        return epics;
    }

    public boolean canEditEpics() {
        return canEditEpics;
    }

    public boolean canManageSprints() {
        return canManageSprints;
    }

    public boolean maxIssuesExceeded() {
        return maxIssuesExceeded;
    }

    public int queryResultLimit() {
        return queryResultLimit;
    }

    public Map<String, List<RapidViewVersion>> getVersionsPerProject() {
        return versionsPerProject;
    }
}
